package com.qiye.pointsserver.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiye.pointsserver.common.PageUtil.PageParam;
import com.qiye.pointsserver.common.PageUtil.PageParams;
import com.qiye.pointsserver.common.PageUtil.PageResult;
import com.qiye.pointsserver.mapper.UserMapper;
import com.qiye.pointsserver.mapper.UserinfoMapper;
import com.qiye.pointsserver.model.entity.Auther;
import com.qiye.pointsserver.model.entity.Pointslog;
import com.qiye.pointsserver.model.entity.User;
import com.qiye.pointsserver.model.entity.Userinfo;
import com.qiye.pointsserver.service.EmailService;
import com.qiye.pointsserver.service.PointsLogRepository;
import com.qiye.pointsserver.service.PointslogService;
import com.qiye.pointsserver.mapper.PointslogMapper;
import jakarta.annotation.Resource;
import jakarta.mail.MessagingException;
import org.apache.ibatis.annotations.UpdateProvider;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Lenovo
* @description 针对表【sys_pointslog(积分日志表)】的数据库操作Service实现
* @createDate 2025-07-09 16:41:28
*/
@Service
public class PointslogServiceImpl extends ServiceImpl<PointslogMapper, Pointslog>
    implements PointslogService{

    @Resource
    private PointslogMapper pointslogMapper;

    @Resource
    private PointsLogRepository pointsLogRepository;


    @Resource
    private EmailService emailService;

    @Resource
    private UserinfoMapper userinfoMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public PageResult<Pointslog> searchPointslog(PageParams pageParam) {
        Page<Pointslog> page = new Page<>(pageParam.getPageNum(), pageParam.getPageSize());
        IPage<Pointslog> resultPage = pointslogMapper.searchPointsLog(page,pageParam.getUserinfoID(), pageParam.getKeyword());
        return new PageResult<>(resultPage);
    }




    public  void emailToPointsEquals7(){
        pointsLogRepository.decrementTimeEveryDay();

        List<Pointslog> pointslogs = pointsLogRepository.findUserIdsWithTimeEquals7();

        for (Pointslog pointslog : pointslogs) {
            sendEmailNotification(pointslog);
        }
        

    }


    @Async
    public void sendEmailNotification(Pointslog pointslog){
        Userinfo userinfo = userinfoMapper.selectById(pointslog.getUserinfoId());

        User user = userMapper.selectById(userinfo.getUserId());

        String subject = "积分即将到期提醒";
        String text = " 尊敬的"+userinfo.getName()+"先生/女士，您好!"
                +"您于我司通过："
                +pointslog.getType()
                +"获取到的"+pointslog.getPoints()+"积分，"
                +"还有："+pointslog.getTime()+"天即将到期，请您及时使用，以免过期失效。";
        try{
            emailService.sendSimpleMessage(user.getEmail(),subject,text);
        } catch (Exception e) {
            System.out.println("邮件发送失败"+e);
        }
    }


}




